import 'dart:convert';

import 'package:ac/api/ac_urls.dart';
import 'package:ac/common/ac_more_util.dart';
import 'package:ac/data/ac_user_manager.dart';
import 'package:ac/model/ac_media_model.dart';
import 'package:ac/model/ac_video_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import 'ac_video_state.dart';

List<String> videoUrls = [
  "livchat/images/dev/20231224/1.mp4",
  "livchat/images/dev/20231224/2.mp4",
  "livchat/images/dev/20231224/3.mp4",
  "livchat/images/dev/20231224/4.mp4",
  "livchat/images/dev/20231224/5.mp4",
  "livchat/images/dev/20231224/6.mp4",
  "livchat/images/dev/20231224/7.mp4",
  "livchat/images/dev/20231224/8.mp4",
  "livchat/images/dev/20231224/9.mp4",
  "livchat/images/dev/20231224/10.mp4",
];

List<String> imageUrls = [
  "livchat/images/dev/20231224/1.mp4",
  "livchat/images/dev/20231224/2.mp4",
  "livchat/images/dev/20231224/3.mp4",
  "livchat/images/dev/20231224/4.mp4",
  "livchat/images/dev/20231224/5.mp4",
  "livchat/images/dev/20231224/6.mp4",
  "livchat/images/dev/20231224/7.mp4",
  "livchat/images/dev/20231224/8.mp4",
  "livchat/images/dev/20231224/9.mp4",
  "livchat/images/dev/20231224/10.mp4",
];

class AcVideoLogic extends GetxController {
  final AcVideoState state = AcVideoState();

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    _initVideoUrl();
  }

  Future<void> _initVideoUrl() async {
    await AcUserManager.initVideoListCompleter.future;
    var list = AcUserManager.videoList;
    list.retainWhere((element) => element.userInfo?.isBlock == false || element.userInfo?.isBlock == null);
    if (list.first.mediaModel == null) {
      var res = await AcUrls.ossSearch.post(
        data: AcUserManager.videoList
            .map((e) => {"mediaPath": e.video, "mediaType": "video"})
            .toList(),
        showLoading: false,
      );
      if (res?["code"] == 0) {
        for (var i = 0; i < list.length; ++i) {
          var o = list[i];
          o.mediaModel = AcMediaModel.fromJson(res?["data"][i]);
        }
        _updateLocalVideoList(list);
      }
    }
    state.videoList.value = list;
  }

  void _updateLocalVideoList(List<AcVideoModel> list) {
    AcUserManager.videoList = list;
    AcUserManager.updateVideos();
  }

  void likeVideo(int index) {
    var video = state.videoList[index];
    video.isLiked = !video.isLiked;
    state.videoList[index] = video;
    state.videoList.refresh();
    _updateLocalVideoList(state.videoList);
  }

  void more(int index,VoidCallback callback) {
    var video = state.videoList[index];
    f(){
      callback();
      _handleBlock();
    }
    var moreUtil = AcMoreUtil(video.userInfo!, state.videoList.refresh,blockCallback: f);
    moreUtil.show();
  }

  sendComment(index,text) {
    if (text.isEmpty) {
      return;
    }
    EasyLoading.show(status: 'sending...', maskType: EasyLoadingMaskType.black);
    Future.delayed(const Duration(milliseconds: 1300), () {
      EasyLoading.dismiss();
      debugPrint('send comment success');
      state.commentList.add(text);
    });
  }



  void _handleBlock() {
    state.videoList[state.currentIndex.value].userInfo?.isBlock = true;
    _updateLocalVideoList(state.videoList);
    state.videoList.removeAt(state.currentIndex.value);
    state.videoList.refresh();
  }
}
